﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithm.Easy_14_LongestCommonPrefix
{
    //最长公共前缀
    class MainProject
    {
        static void Main(string[] args)
        {
            string[] strs = new string[] { "flower", "flow", "flight" };
            Console.WriteLine(LongestCommonPrefix(strs));
            Console.ReadKey();
        }


        public static string Recursion(int index, string[] strs, string cont)
        {
            try
            {
                char tempChar = strs[0][index];
                for (int i = 0; i < strs.Length; i++)
                {
                    if (tempChar != strs[i][index])
                    {
                        return cont;
                    }
                    if (i == strs.Length - 1)
                    {
                        cont += tempChar;
                        index++;
                        return Recursion(index, strs, cont);
                    }
                }
                return cont;
            }
            catch (Exception)
            {
                return cont;
            }
        }

        public static string LongestCommonPrefix(string[] strs)
        {
            string cont = string.Empty;
            int index = 0;
            return Recursion(index, strs, cont);
        }
    }
}
